package com.atguigu.controller;

import com.atguigu.en.YesOrNoEnum;
import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @DubboReference
    private UserInfoService userInfoService;

    @GetMapping("/sendCode/{phone}")
    public Result getCode(@PathVariable("phone") Long phone, HttpSession session) {
        //1.调用阿里云的API，给phone发送一个随机验证码
        //进行模拟
        String code = "1111";
        //2.将发送给phone的验证码存储到redis，并且设置时效性;或者存储到session
        session.setAttribute("CODE", code);
        return Result.ok();
    }

    //注册
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo, HttpSession session) {
        //1.检验验证码是否正确
        //1.1获取用户输入的验证码
        String code = registerVo.getCode();
        //1.2获取服务器保存的验证码
        String sessionCode = (String) session.getAttribute("CODE");
        if (!code.equalsIgnoreCase(sessionCode)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
        //2.校验手机号是否被占用(调用业务层方法根据手机号查询用户,若查到表示占用了)
        String phone = registerVo.getPhone();
        if (!ObjectUtils.isEmpty(userInfoService.getByPhone(phone))) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

        //3.校验昵称是否被占用(调用业务层方法根据昵称查询用户,若查到表示占用了)
        String nickName = registerVo.getNickName();
        if (!ObjectUtils.isEmpty(userInfoService.getByNickname(nickName))) {
            return Result.build(null, ResultCodeEnum.NICKNAME_REGISTER_ERROR);
        }
        //4.对密码进行加密
        registerVo.setPassword(MD5.encrypt(registerVo.getPassword()));
        //5.将数据保存到数据库
        UserInfo userInfo = new UserInfo();
        //属性对拷(两个对象里面相同名字的属性对拷),RegisterVo对象里的3个属性已有值，拷给UserInfo对应的属性
        BeanUtils.copyProperties(registerVo, userInfo);
        //设置status：账号是否被锁定
        userInfo.setStatus(YesOrNoEnum.YES.getCode());
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpSession session) {
        //1.校验手机号是否正确
        String phone = loginVo.getPhone();
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if (ObjectUtils.isEmpty(userInfo)) {
            //如果查不到，说明手机号错误
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
        //2.校验密码是否正确
        String password = loginVo.getPassword();//用户输入的密码(未加密)
        String dbPassword = userInfo.getPassword();//数据库中的密码(已加密)
        if (!dbPassword.equals(MD5.encrypt(password))) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
        //3.检验账号是否被锁定
        if (userInfo.getStatus() == YesOrNoEnum.NO.getCode()) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
        //4.登录成功，将登录的用户保存到session中
        session.setAttribute("USER", userInfo);
        //5.将用户的nickName和phone响应给客户
        Map resultMap = new HashMap<>();
        resultMap.put("nickName", userInfo.getNickName());
        resultMap.put("phone", userInfo.getPhone());
        return Result.ok(resultMap);
    }

    @GetMapping("/logout")
    public Result logout(HttpSession session) {
        session.invalidate();
        return Result.ok();
    }


}
